package com.answerz.module_reportissues.adapter

import android.os.Bundle
import android.view.View
import com.answerz.module_reportissues.R
import com.answerz.module_reportissues.databinding.ItemReportBinding
import com.answerz.module_reportissues.fragment.ReportListFragment
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.ReportListBean
import com.czl.base.util.DialogHelper

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2021/12/10 11:38
 */
class ReportAdapter(private val fragment: ReportListFragment) :
    BaseQuickAdapter<ReportListBean.Data, BaseDataBindingHolder<ItemReportBinding>>(
        R.layout.item_report
    ) {

    var currentView: View? = null

    override fun convert(
        holder: BaseDataBindingHolder<ItemReportBinding>,
        item: ReportListBean.Data
    ) {
        holder.dataBinding?.apply {
            data = item
            adapter = this@ReportAdapter
            executePendingBindings()
        }
    }

    val onItemClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is ReportListBean.Data) {
            dealOption("查看", it.no)
        }
    })

    val onRenewClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is ReportListBean.Data) {
            DialogHelper.showMoreDialog(
                context,
                currentView!!,
                when (it.status) {
                    "待接单", "处理中" -> arrayOf("查看", "撤销")
                    "待支付" -> arrayOf("查看", "支付")
                    "待评价" -> arrayOf("查看", "评价", "删除")
                    "已评价" -> arrayOf("查看", "删除")
                    "待审核" -> arrayOf("查看")
                    else -> arrayOf("")
                }
            ) { _, text ->
                dealOption(text, it.no)
            }

        }
    })

    val onCurrentDot: BindingCommand<View> = BindingCommand(BindingConsumer {
        currentView = it
    })

    private fun dealOption(text: String, no: String) {
        when (text) {
            "查看" -> fragment.startContainerActivity(
                AppConstants.Router.Report.F_ORDER_DETAIL,
                Bundle().apply {
                    putString(
                        AppConstants.BundleKey.WORK_ORDER_NO,
                        no
                    )
                })
            "撤销" -> fragment.viewModel.uc.cancelOrderEvent.postValue(no)
            "支付" -> fragment.viewModel.uc.navPayEvent.postValue(no)
            "评价" -> fragment.viewModel.uc.navScoreEvent.postValue(no)
            "删除" -> fragment.viewModel.uc.deleteOrderEvent.postValue(no)
        }
    }
}
